package com.fwmagic.spark.core.cases.globalsort

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  */
object CustomSort4 {
    def main(args: Array[String]): Unit = {
        val conf: SparkConf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[*]")

        val sc = new SparkContext(conf)

        //姓名，年龄，颜值
        //根据颜值降序，年龄升序排序
        val rdd: RDD[String] = sc.parallelize(List("laoduan,30,99.99","laozhao,18,9999.99","nianhang,29,99.99"))

        val rdd1: RDD[CaseMan] = rdd.map(line => {
            val fields: Array[String] = line.split(",")
            val name: String = fields(0)
            val age: Int = fields(1).toInt
            val faceValue: Double = fields(2).toDouble
            CaseMan(name,age,faceValue)
        })

        import SortContext.manSortRules

        val sorted: RDD[CaseMan] = rdd1.sortBy(cm => cm)

        val boys: Array[CaseMan] = sorted.collect()

        println(boys.toBuffer)

        sc.stop()

    }

}
